﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace wlog.Model
{
    public class OutputChannel
    {
        public List<string> Lines { get; set; }
        public object Data { get; set; }
        public Exception Error = null;
        public delegate void AddLine(string line);
        // Define an Event based on the above Delegate
        public event AddLine OnAddLine;
        // TODO : lock
        public static string resultLock = "moresreultlock";
        public OutputChannel()
        {
            Lines = new List<string>();
        }
        public void WriteLine(string format, params object[] args)
        {
            string line = string.Format(format, args);
            lock (resultLock)
            {

                Lines.Add(line);
            }
            if (OnAddLine != null)
            {
                OnAddLine(line);
            }
        }
        public void WriteLine(string message)
        {
            lock (resultLock)
            {
                Lines.Add(message);
            }
            if (OnAddLine != null)
            {
                OnAddLine(message);
            }
        }
        public void Add(string message)
        {
            lock (resultLock)
            {
                Lines.Add(message);
            }
            if (OnAddLine != null)
            {
                OnAddLine(message);
            }
        }
        public void WriteKeyPair(string key, object value, int space = 30)
        {
            WriteLine("{0}\t{1}", key.PadRight(space), value);
        }
    }
}
